﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FTchina;
using FTchina.Library;
using FineUI;
using FTchina.Logic;
using PWMIS.DataMap.Entity;
using System.Collections.Generic;
using FTchina.Models;
public partial class UI_login : Page
{
    string LoginType = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            LoginType = FTchina.basic.SafeRequest(Request["LoginType"]);
            RequestHelper.WriteCookie("CaptchaImageText", StringHelper.getRandomNumber(6));
            tbxCaptcha.Text = RequestHelper.GetCookie("CaptchaImageText");
            // tbxPassword.Text = "admin";
            // tbxUserName.Text = "admin";
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (tbxCaptcha.Text == RequestHelper.GetCookie("CaptchaImageText"))
        {
            Chkuser(RequestHelper.SafeRequest(tbxUserName.Text), RequestHelper.SafeRequest(tbxPassword.Text));
        }
        else
        {
            Alert.ShowInParent("验证码输入错误");
        }

    }





    public void Chkuser(string name, string password)
    {
        bool IsLocalLogin = true;//是否本地登录，本地登录不需要验证门户

        IsLocalLogin = RequestHelper.SafeRequest(Request["type"]) == "local" ? true : false;

        if (!IsLocalLogin)
        {//不是本地登录
            //验证是否登录门户账户
            FTchina.OA.OALogin oal = new FTchina.OA.OALogin(name, password);
            bool LoginResult = false;
            if (System.Configuration.ConfigurationManager.AppSettings["IsLdapLogin"].ToString() == "true")
            {//ldap
                LoginResult = oal.IsLoginByLdap;
            }
            else
            {//模拟门户
                LoginResult = oal.IsLogin;
            }
            if (!LoginResult)
            {
                AlertLogic.ShowResultAndRedirect("门户用户名或者密码错误", "错误", MessageBoxIcon.Error, "/ui/login.aspx", true);
                return;
            }

             
        }

        string pass = SecurityHelper.EncryptMD5(password);

        FTchina.Models.Master mMaster = new FTchina.Models.Master();
        FTchina.Models.UserList mUserList = new FTchina.Models.UserList();

        OQLCompare cmp = new OQLCompare(mMaster);
        OQLCompare cmpResult = (cmp.Comparer(mMaster.Nicheng, OQLCompare.CompareType.Equal, name));

        if (IsLocalLogin)
        {//本地登录要验证本地密码
            cmpResult = cmpResult & (cmp.Comparer(mMaster.Master_Password, OQLCompare.CompareType.Equal, pass));
        }

        OQL q = new OQL(mMaster);
        q.Select(mMaster.Usergroup, mMaster.Nicheng, mMaster.State, mMaster.Master_Password, mMaster.Link).Where(cmpResult);
        q.Join(mUserList).On(mMaster.Link, mUserList.Id);



        List<FTchina.Models.Master> masterlist = EntityQuery<FTchina.Models.Master>.QueryList(q, mMaster);

        if (masterlist.Count == 0)
        {

            FTchina.Library.LogHelper.Warn("用户:" + name + " 登陆失败,尝试的密码是:" + password + "IP地址:" + RequestHelper.GetUserIp());

            Alert.ShowInParent("请联系系统管理员注册");

        }
        else
        {
            foreach (FTchina.Models.Master m in masterlist)
            {

                if (m.State == "1")
                {//账户状态正常


                    mUserList.Id = Convert.ToInt32(m.Link);//获取指定ID的UserList
                    EntityQuery<FTchina.Models.UserList> qUserlist = new EntityQuery<FTchina.Models.UserList>(mUserList);
                    qUserlist.FillEntity(mUserList);

                   

                    MasterHelper.ParentItemName = UserListLogic.getParentItemNameByParentID(mUserList.ParentID);

                    MasterHelper.UName = mUserList.UName;//用户组织架构的姓名
                    MasterHelper.UID = mUserList.UID;
                    MasterHelper.MasterID = SecurityHelper.EncryptMD5(FTchina.basic.GetGuid());
                    MasterHelper.UserGroup = m.Usergroup;
                    MasterHelper.UserGroupName = LoginLogic.GetUserGroupNameByID(m.Usergroup);
                    MasterHelper.Nickname = m.Nicheng;
                    MasterHelper.Luruid = m.Link;
                    MasterHelper.LoginIP = RequestHelper.GetUserIp();
                    string Area_Name = string.Empty;
                    MasterHelper.BssAreaCode = LoginLogic.GetBssAreaCodeByName(MasterHelper.ParentItemName,out Area_Name);
                    MasterHelper.EssAreaName = Area_Name;

                    FTchina.Library.LogHelper.Info("用户:" + name + MasterHelper.LoginIP + "登陆成功");
                    #region 插入登录日志
                    FTchina.Models.Login_Log mLogin_Log = new FTchina.Models.Login_Log();
                    mLogin_Log.Id = 0;
                    mLogin_Log.UName = mUserList.UName;
                    mLogin_Log.UID = mUserList.UID;
                    mLogin_Log.GroupName = MasterHelper.UserGroupName;
                    mLogin_Log.Ip = RequestHelper.GetUserIp();
                    mLogin_Log.LoginDate = DateTime.Now;
                    FTchina.BLL.Login_LogManager.AddOrUpdateLogin_Log(mLogin_Log);
                    #endregion
                    Response.Redirect("/default.aspx");
                    //AlertLogic.ShowResultAndRedirect("登录成功", "恭喜", "/default.aspx", true);
                }
                else
                {
                    Alert.ShowInParent("您的帐户已被锁定，请与系统管理员联系！");

                }
            }
        }



    }


    protected void btnDown_Click(object sender, EventArgs e)
    {
        Response.AppendHeader("Content-Disposition", "attachment;filename=aoyou.zip");

        Response.TransmitFile("/aoyou.zip");
    }


}